Sending data to a local database with an ESP and Raspberry PI |
|
1. Raspberry PI |
|
Working with SSH:
Making your IP-address static:
Interface wlan0 // here you specify that you work with wifi static_routers=[router ip] // routers IP address static domain_name_server = [dns-ip] // your dns-server static ip-address=[address you want]/24 //the ip you want to make static
$v sudo ip r
$ grep “nameserver” /etc/resolv.conf |
|
2. Setting up a database |
|
Setting up a database:
|
|
Integrating the project to get temperature values from an ESP8266:
Sending information:
http://[ip-addres pi]/LOGGING/temperature.php?t=x
|
<?php $t = $_GET['t']; $servername = "localhost"; $username = "root"; $password = "your password"; $dbname = "LOGGING"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "INSERT INTO temperature (temp) VALUES ($t)"; if (mysqli_query($conn, $sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } mysqli_close($conn); ?> |
AARON FEYS |
EURASMUS - 01 |
Sending data to a local database with an ESP and Raspberry PI |
|
3. ESP8266 |
|
#include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> #include <WiFiClient.h> const char* ssid = "HoeveLanghof"; const char* password = "vakantie"; //Your Domain name with URL path or IP address with path String serverName = "http://192.168.1.143/LOGGING/temperature.php"; // the following variables are unsigned longs because the time, measured in // milliseconds, will quickly become a bigger number than can be stored in an int. unsigned long lastTime = 0; unsigned long timerDelay = 5000;
void setup() { Serial.begin(115200); WiFi.begin(ssid, password); Serial.println("Connecting"); while(WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to WiFi network with IP Address: "); Serial.println(WiFi.localIP()); Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading."); } void loop() { // Send an HTTP POST request depending on timerDelay if ((millis() - lastTime) > timerDelay) { int t = random(30); //radom generated temperature value //Check WiFi connection status if(WiFi.status()== WL_CONNECTED){ WiFiClient client; HTTPClient http;String serverPath = serverName + "?t=" + t; // Your Domain name with URL path or IP address with path http.begin(client, serverPath.c_str()); // Send HTTP GET request int httpResponseCode = http.GET();
|
|
AARON FEYS |
EURASMUS - 02 |
Sending data to a local database with an ESP and Raspberry PI |
|
if (httpResponseCode>0) { Serial.print("HTTP Response code: "); Serial.println(httpResponseCode); String payload = http.getString(); Serial.println(payload); } else { Serial.print("Error code: "); Serial.println(httpResponseCode); } // Free resources http.end(); } else { Serial.println("WiFi Disconnected"); } lastTime = millis(); } } |
|
Code:
|
|
AARON FEYS |
EURASMUS - 03 |